# jython 

from oracle.jdbc.pool import OracleDataSource

oracleDataSource = OracleDataSource()
oracleDataSource.setURL('dbc:oracle:thin:@192.168.63.112:1521:EUDB')

#from java.sql import Connection
connection = oracleDataSource.getConnection("euidb", "test")

metaData = connection.getMetaData()

from java.sql import ResultSetMetaData

def procedures(schemaPattern = 'EUIDB'):
  if schemaPattern is None:
    schemaPattern = "%"
  p = metaData.getProcedures(None, schemaPattern, "%")
  resultSetMetaData = p.getMetaData()
  columns = resultSetMetaData.getColumnCount()
  while p.next():
    result = ""
    for i in range(1, columns + 1):
      result = "%s %s" %  (result, p.getObject(i))
    print result

#  returns java.sql.ResultSet 
def getProcedures(schemaPattern = 'EUIDB' ):
  if schemaPattern is None:
    schemaPattern = "%"
  procedures = metaData.getProcedures(None, schemaPattern, "%")
  return procedures

# output description of generic java.sql.ResultSet
def describeResultSet(resultSet):
  rsMetaData = resultSet.getMetaData()
  columns = rsMetaData.getColumnCount()

  column_name = [None]
  for i in range(1, columns + 1):
    column_name.append(rsMetaData.getColumnName(i))
  print "%r" % column_name

  column_type = [None]
  for i in range(1, columns + 1):
    column_type.append(rsMetaData.getColumnTypeName(i))
  print "%r" % column_type

  while resultSet.next():
    result = ""
    for i in range(1, columns + 1):
      result += "|%s|" % resultSet.getString(i)
    print result

def describeProcedure(catalog = 'PASSWD_CHANGE', 
                      schema = 'EUIDB', 
                      procedureNamePattern = 'WEB_PASSWD_CHG_OCCURRED',
                      catalogNamePattern = None):
  return metaData.getProcedureColumns(catalog, schema, 
                                      procedureNamePattern, 
                                      catalogNamePattern)



# from JDK-1.5.0 java.sql.DatabaseMetaData
columnTypes = {
  'procedureColumnUnknown': 0,
  'procedureColumnIn' : 1,
  'procedureColumnInOut': 2,
  'procedureColumnOut': 4,
  'procedureColumnReturn': 5,
  'procedureColumnResult': 3
  }

# interpret resultSet returned from 
# java.sql.DatabaseMetaData.getProcedureColumns() as 
def getProcedureSignature(rs):
  getID = lambda rs: (rs.getString(1), rs.getString(2), rs.getString(3))
  catalogSchemaName = None
  COLUMN_NAME = 4
  COLUMN_TYPE = 5
  TYPE_NAME   = 7

  (input, output) = ([], [])

  while rs.next():
    if catalogSchemaName is None:
      catalogSchemaName = getID(rs)
    elif catalogSchemaName != getID(rs):
      print "ERROR: " + repr(catalogSchemaName) \
            + " doesn't match " + repr(getID(rs))
    type = rs.getShort(COLUMN_TYPE)

    # XXX need to Out values as well somehow in the return type 
    if type == columnTypes['procedureColumnReturn']:
      output.append([None, rs.getString(TYPE_NAME)])
    elif type in [columnTypes['procedureColumnInOut'],
                  columnTypes['procedureColumnIn']]:
      input.append([rs.getString(COLUMN_NAME), rs.getString(TYPE_NAME)])

  return (input, output)

import nu.xom.Element;

document = None

def initWSDL():
  global root, document
  root = nu.xom.Element("description");
#  document = 

def addMethod(input, output):
  None
  
    

      


  
